package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.EmployeeService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.MD5;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeContorller {

    @Autowired
    private EmployeeService employeeService;

    //展示列表
    @PatchMapping("/loadAll")
    private List<Employee> loadAll(){

        return employeeService.loadAll();

    }

    @GetMapping("{id}")
    private Employee loadById(@PathVariable("id") Long id){
        Employee employee = employeeService.loadById(id);
        return employee;

    }

    //新增
    @PutMapping
    private AjaxResult save(@RequestBody Employee employee){
        try {
            String passwordd = employee.getPassword();
            String password = MD5.md5(passwordd, "MD5");
            employee.setPassword(password);
            employeeService.save(employee);
             return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"保存失败"+e.getMessage());
        }

    }

    //修改
    @PostMapping
    private AjaxResult update(@RequestBody Employee employee){
        try {
            String passwordd = employee.getPassword();
            String password = MD5.md5(passwordd, "MD5");
            employee.setPassword(password);
            employeeService.update(employee);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"修改失败"+e.getMessage());
        }
    }

    //删除
    @DeleteMapping("{id}")
    private AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }

    //批量删除
    @PatchMapping("/batchRemove")
    private AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }

    //展示f分页列表
    @PatchMapping
    private PageList<Employee> loadPageByQuery(@RequestBody EmployeeQuery query){

        return employeeService.loadPageByQuery(query);

    }
}
